package com.nhjf.admin.controller.incomeAndExpenditureRecordd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.nhjf.admin.util.Jurisdiction;
import com.nhjf.common.page.Pagination;
import com.nhjf.model.incomeAndExpenditureRecord.IncomeAndExpenditureRecordd;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.incomeAndExpenditureRecordd.IncomeAndExpenditureRecorddService;
import com.nhjf.service.teachers.TeachersService;
@RequestMapping("/incomeAndExpenditureRecordd")
@Controller
public class IncomeAndExpenditureRecorddController {
	@Autowired
	private IncomeAndExpenditureRecorddService andExpenditureRecorddService;
	@Autowired
	private TeachersService teacherService;

	/**
	 * 账户列表
	 * @param response
	 * @param request
	 * @param model
	 * @param page
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/incomeAndExpenditureRecordd_Page")
	public String incomeAndExpenditureRecordd_Page(HttpServletResponse response,HttpServletRequest request, Model model, @RequestParam(value="page", defaultValue="1")Integer page) throws ParseException {
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		TeachersQo teachersQo=new TeachersQo();
		teachersQo.setSeachOrganization(true);
		teachersQo.setSeachUser(true);
		teachersQo.setUserId(user.getId());
		Teacher teacher=teacherService.queryUnique(teachersQo);
		if(teacher==null) {
			return "500";
		}
		IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
		andExpenditureRecorddQo.setSeachOranizationAccount(true);
		andExpenditureRecorddQo.setSeachPayLog(true);
		andExpenditureRecorddQo.setOranizationAccountId(teacher.getTraininginstitution().getId());
		SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		String nian=String.valueOf(year);
		int month = cal.get(Calendar.MONTH )+1;
		String yue=String.valueOf(month);
		String nianyue=nian+"-"+yue+"-00 00:00:00";
		andExpenditureRecorddQo.setCreaTime(format1.parse(nianyue));
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		pagination.setCondition(andExpenditureRecorddQo);
		pagination.setPageNo(page);
		pagination = andExpenditureRecorddService.queryPagination(pagination);
		IncomeAndExpenditureRecordd andExpenditureRecordd = andExpenditureRecorddService.queryUnique(andExpenditureRecorddQo);
		if(andExpenditureRecordd!=null) {
			model.addAttribute("pendingSettlementAmount", andExpenditureRecordd.getOranizationAccount().getPendingSettlementAmount());
		}
		model.addAttribute("pagination", pagination);
		return "/incomeAndExpenditureRecordd/andExpenditureRecordd_list.html";
	}
	@RequestMapping("/incomeAndExpenditureRecordd_View")
	public String incomeAndExpenditureRecordd_View(Model model,String id) {
		IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
		andExpenditureRecorddQo.setSeachOranizationAccount(true);
		andExpenditureRecorddQo.setSeachPayLog(true);
		andExpenditureRecorddQo.setIncomeAndExpenditureRecorddId(Long.parseLong(id));
		IncomeAndExpenditureRecordd andExpenditureRecordd=andExpenditureRecorddService.queryUnique(andExpenditureRecorddQo);
		model.addAttribute("andExpenditureRecordd",andExpenditureRecordd);
		return "/incomeAndExpenditureRecordd/andExpenditureRecordd_view.html";
	}
	@RequestMapping("/accountBalance_Page")
	public String accountBalance(HttpServletResponse response,HttpServletRequest request, Model model, @RequestParam(value="page", defaultValue="1")Integer page) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.MONTH,curr.get(Calendar.MONTH)-1);
		Date date=curr.getTime();
		c.setTime(date);
		int month = c.get(Calendar.MONTH)+1; // 上个月月份
		String months=String.valueOf(month);
		int year = c.get(Calendar.YEAR);
		String nian=String.valueOf(year);
		int day = c.getActualMaximum(Calendar.DAY_OF_MONTH); // 结束天数
		String days=String.valueOf(day);
		String theLastDay=nian+"-"+months+"-"+days+" 23:59:59";
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		TeachersQo teachersQo=new TeachersQo();
		teachersQo.setSeachOrganization(true);
		teachersQo.setSeachUser(true);
		teachersQo.setUserId(user.getId());
		Teacher teacher=teacherService.queryUnique(teachersQo);
		if(teacher==null) {
			return "500";
		}
		IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
		andExpenditureRecorddQo.setSeachOranizationAccount(true);
		andExpenditureRecorddQo.setSeachPayLog(true);
		andExpenditureRecorddQo.setOranizationAccountId(teacher.getTraininginstitution().getId());
		andExpenditureRecorddQo.setMyBalanceTime(format.parse(theLastDay));
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		pagination.setCondition(andExpenditureRecorddQo);
		pagination.setPageNo(page);
		pagination = andExpenditureRecorddService.queryPagination(pagination);
		IncomeAndExpenditureRecordd andExpenditureRecordd = andExpenditureRecorddService.queryUnique(andExpenditureRecorddQo);
		if(andExpenditureRecordd!=null) {
			model.addAttribute("amount", andExpenditureRecordd.getOranizationAccount().getAmount());
		}
		model.addAttribute("pagination", pagination);
		return "/incomeAndExpenditureRecordd/accountBalance_list.html";
	}
	@RequestMapping("/accountBalance_View")
	public String accountBalance_View(Model model,String id) {
		IncomeAndExpenditureRecorddQo andExpenditureRecorddQo=new IncomeAndExpenditureRecorddQo();
		andExpenditureRecorddQo.setSeachOranizationAccount(true);
		andExpenditureRecorddQo.setSeachPayLog(true);
		andExpenditureRecorddQo.setIncomeAndExpenditureRecorddId(Long.parseLong(id));
		IncomeAndExpenditureRecordd andExpenditureRecordd=andExpenditureRecorddService.queryUnique(andExpenditureRecorddQo);
		model.addAttribute("andExpenditureRecordd",andExpenditureRecordd);
		return "/incomeAndExpenditureRecordd/accountBalance_view.html";
	}
}
